package com.hrt.manage.member.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.base.BaseController;
import com.hrt.bean.HRTMember;
import com.hrt.manage.member.service.HRTMemberService;
import com.hrt.util.GetAgeUtil;
import com.hrt.util.MD5Tool;

@Controller
@RequestMapping(value = "/manage/member")
public class HRTMemberController extends BaseController<HRTMember> {
	@Autowired
	private HRTMemberService hrtMemberService;

	public HRTMemberController() {

	}

	public HRTMemberService getHrtMemberService() {
		return hrtMemberService;
	}

	public void setHrtMemberService(HRTMemberService hrtMemberService) {
		this.hrtMemberService = hrtMemberService;
	}

	public List<HRTMember> getList() {
		return list;
	}

	public void setList(List<HRTMember> list) {
		this.list = list;
	}

	/**
	 * 
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/list")
	public ModelAndView list(HttpServletRequest request) {
		ModelAndView view = new ModelAndView();
		System.out.println("list");
		list = hrtMemberService.queryByPage(getPageIndex(), getPageSize());
		setTotalCount(hrtMemberService.totalCount());
		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));

		page = this.getPage();

		view.addObject("pageResult", page);
		// request.getSession().setAttribute("pageResult", page);
		return view;

	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody Long[] delete(
			@RequestParam(value = "ids", required = true) String ids,
			HttpServletResponse response) throws IOException {
		Long[] idArr = null;
		ModelAndView view = new ModelAndView();

		if (ids != null) {
			if (ids.indexOf(",") != -1) {
				String[] idStrs = ids.split(",");
				idArr = new Long[idStrs.length];
				for (int idx = 0; idx < idStrs.length; idx++) {
					idArr[idx] = Long.parseLong(idStrs[idx]);
				}
			} else {
				idArr = new Long[] { Long.parseLong(ids) };
			}

		}
		try {
			hrtMemberService.deleteById(idArr);

			return idArr;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	@RequestMapping(value = "/save.do", method = RequestMethod.POST)
	public void save(@ModelAttribute("memberInfo") HRTMember member,
			HttpServletRequest request, HttpServletResponse response)
	{
		String password = "111111";
		String borth = request.getParameter("born");
		String pasd = MD5Tool.MD5(password);
		Date registTime = new Date();
		// 根据生日计算年龄
		int age = GetAgeUtil.getAge(borth, GetAgeUtil.getStringDate());
		member.setAge(age);
		// 默认设置密码位“111111”
		member.setPassword(pasd);
		// 设置添加时间
		member.setRegisterTime(registTime);
		// 默认设置激活
		member.setIsMailVaild(1);
		
		try {
			hrtMemberService.saveMember(member);

			response.sendRedirect("list.do");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@RequestMapping(value = "/show")
	@ResponseBody
	public Map show(
			@RequestParam(value = "id", required = true) String memberId,
			HttpServletRequest request, HttpServletResponse response) {

		Map map = new HashMap();
		
		if (memberId != null && !"".equals(memberId)) {
			HRTMember member = this.hrtMemberService.getMemberByid(new Long(
					memberId));
			if (member != null) {
				map = transBean2Map(member);
			}

		}
		
		return map;

	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public void update(@ModelAttribute("memberInfo") HRTMember member,
			HttpServletRequest request, HttpServletResponse response) {
		String mbrName = member.getMbrName();
		String mail = member.getMail();
		String mobile = member.getMobile();
		String note = member.getNote();
		Integer sex = member.getSex();
		Integer statu = member.getStatu();
		String borth = request.getParameter("born");
		// 根据生日计算年龄
		int age = GetAgeUtil.getAge(borth, GetAgeUtil.getStringDate());
		member = this.hrtMemberService.getMemberByid(new Long(member.getId()));
		if(member!=null){
			
			member.setAge(age);
			member.setMbrName(mbrName);
			member.setMail(mail);
			member.setNote(note);
			member.setMobile(mobile);
			member.setSex(sex);
			member.setBorn(member.getBorn());
			member.setStatu(statu);
			hrtMemberService.updateMember(member);
		}

		try {
			response.sendRedirect("list.do");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 
	 * @param pageIndex
	 * 
	 * @param pageSize
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/queryByPage")
	@ResponseBody
	public Map queryByPage(
			@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			HttpServletRequest request) {
		Map resultMap = new HashMap();

		if (pageSize == null || "".equals(pageSize)) {
			pageSize = 30;
		}
		if (pageIndex == null || "".equals(pageIndex)) {
			pageIndex = 1;
		}
		setPageIndex(pageIndex);

		setPageSize(pageSize);

		setTotalCount(hrtMemberService.totalCount());

		List<HRTMember> resultList = hrtMemberService.queryByPage(
				getPageIndex(), getPageSize());
		setList(resultList);

		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));

		page = this.getPage();

		resultMap.put("pageResult", page);
		return resultMap;
	}
	/**
	 * 检查会员名
	 * @param param
	 * @param username
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/checkMemberName", method = RequestMethod.POST)
	@ResponseBody
	public Map checkMemberName(
			@RequestParam(value = "memberId", required = false) Long memberId,
			@RequestParam(value = "param", required = true) String param,
			@RequestParam(value = "name", required = true) String username,
			HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String name = null;
		HRTMember oldmember = null;
		
		HRTMember member = this.hrtMemberService.getMemberByName(param);
		if(memberId!=null){
			oldmember = this.hrtMemberService.getMemberByid(memberId);
			
			name = oldmember.getMbrName();
			
			if(param.equals(name)){
				if (member != null) {
					map.put("status", "y");
					map.put("info", "会员名验证通过");
				} else {
					map.put("status", "n");
					map.put("info", "会员名已经被使用");

				}
			}
			else{
				
				if (member != null) {
					map.put("status", "n");
					map.put("info", "会员名已经被使用");
				} else {
					map.put("status", "y");
					map.put("info", "会员名验证通过");
					
				}
			}
		}
		else{
			if (member != null) {
				map.put("status", "n");
				map.put("info", "会员名已经被使用");
			} else {
				map.put("status", "y");
				map.put("info", "会员名验证通过");
				
			}
			
		}
		return map;
	}
	/**
	 * 检查邮件
	 * @param param
	 * @param mail
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/checkMail", method = RequestMethod.POST)
	@ResponseBody
	public Map checkMail(
			@RequestParam(value = "memberId", required = false) Long memberId,
			@RequestParam(value = "param", required = true) String param,
			@RequestParam(value = "name", required = true) String mail,
			HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String oldMail = null;
		HRTMember oldmember = null;
		HRTMember member = this.hrtMemberService.getByMail(param);
		if(memberId!=null){
			oldmember = this.hrtMemberService.getMemberByid(memberId);
			
			oldMail = oldmember.getMail();
			
			if(param.equals(oldMail)){
				if (member != null) {
					map.put("status", "y");
					map.put("info", "邮箱验证通过");
				} else {
					map.put("status", "n");
					map.put("info", "邮箱已经被使用");

				}
			}
			else{
				
				if (member != null) {
					map.put("status", "n");
					map.put("info", "邮箱已经被使用");
				} else {
					map.put("status", "y");
					map.put("info", "邮箱验证通过");
					
				}
			}
		}
		else{
			if (member != null) {
				map.put("status", "n");
				map.put("info", "邮箱已经被使用");
			} else {
				map.put("status", "y");
				map.put("info", "邮箱验证通过");
				
			}
			
		}
		
		
		return map;
	}
	/**
	 * 检查手机号码
	 * @param param
	 * @param mobile
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/validateMobile", method = RequestMethod.POST)
	@ResponseBody
	public Map validateMobile(
			@RequestParam(value = "memberId", required = false) Long memberId,
			@RequestParam(value = "param", required = true) String param,
			@RequestParam(value = "name", required = true) String mobile,
			HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String oldMobile = null;
		HRTMember oldmember = null;
		HRTMember member = this.hrtMemberService.findMobile(param);
		if(memberId!=null){
			oldmember = this.hrtMemberService.getMemberByid(memberId);
			
			oldMobile = oldmember.getMobile();
			
			if(param.equals(oldMobile)){
				if (member != null) {
					map.put("status", "y");
					map.put("info", "手机号码验证通过");
				} else {
					map.put("status", "n");
					map.put("info", "手机号码已经被使用");

				}
			}
			else{
				
				if (member != null) {
					map.put("status", "n");
					map.put("info", "手机号码已经被使用");
				} else {
					map.put("status", "y");
					map.put("info", "手机号码验证通过");
					
				}
			}
		}
		else{
			if (member != null) {
				map.put("status", "n");
				map.put("info", "手机号码已经被使用");
			} else {
				map.put("status", "y");
				map.put("info", "手机号码验证通过");
				
			}
			
		}
		
		return map;
	}
	
}





